package com.zhudeshuai.zhudemolist.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhu.zhuCore.utils.UIUtil;

import java.io.File;
import java.util.List;

/**
 * @author zhudeshuai
 * @since 2018/7/11
 */

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.ViewHolder> {

	private final Context mContext;

	private ItemClickListener mItemClickListener;

	private List<File> data;

	public void setData(List<File> data) {
		this.data = data;
	}

	public PlayListAdapter(Context context) {
		mContext = context;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		TextView itemTv = new TextView(mContext);
		itemTv.setGravity(Gravity.CENTER_VERTICAL);
		itemTv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
		itemTv.setTextColor(Color.parseColor("#191919"));
		itemTv.setPadding(UIUtil.dip2px(15), UIUtil.dip2px(15), UIUtil.dip2px(15), UIUtil.dip2px(15));
		return new ViewHolder(itemTv);
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		final File file = data.get(position);
		TextView itemView = (TextView) holder.itemView;
		itemView.setText(file.getName());
		itemView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mItemClickListener != null) {
					mItemClickListener.onItemClick(file);
				}
			}
		});
	}

	@Override
	public int getItemCount() {
		return data == null ? 0 : data.size();
	}

	public void setItemClickListener(ItemClickListener itemClickListener) {
		mItemClickListener = itemClickListener;
	}

	class ViewHolder extends RecyclerView.ViewHolder {

		ViewHolder(View itemView) {
			super(itemView);
		}
	}


	public interface ItemClickListener {
		void onItemClick(File name);
	}


}
